<?php
function responseEvent($msg)
{
    echo json_encode($msg);
    global $link;
    $link->close();
    exit();//相当于js的return flase;
}

function mySelectFn($selectSqlStr)
{
    global $link;
    $result = $link->query($selectSqlStr);
    $students = [];
    while ($row = $result->fetch_assoc()) {
        array_push($students, $row);
    }
    responseEvent($students);
}

function judge($selecuSqlStr)
{
    global $link;
    $result = $link->query($selecuSqlStr);
    $arr = [];
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            array_push($arr, $row);
        }
    }
    responseEvent($arr);
}

function getSelectArr($selecuSqlStr)
{
    global $link;
    $result = $link->query($selecuSqlStr);
    $arr = [];
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            array_push($arr, $row);
        }
    }
    return $arr;
}

function shopCartEvent()
{
    $id = $_POST['id'];
    $num = $_POST['num'];
    if (isset($_SESSION['loginUserName'])) {
        $name = $_SESSION['loginUserName'];
    } else {
        $msg = ['msg' => '请先登录！', 'status' => 0];
        responseEvent($msg);
    }
    $add_time = time() * 1000;
    $selectPro = 'select * from shop_cart where username="' . $name . '" and pro_id= ' . $id;
    global $link;
    $res = $link->query($selectPro);
    if ($res->num_rows > 0) {
        $arr = [];
        while ($row = $res->fetch_assoc()) {
            array_push($arr, $row);
        }
        $num = $arr[0]['num'] + $num;
        $shopCartId = $arr[0]['id'];
        $updateNum = 'update shop_cart set num=' . $num . ' where id=' . $shopCartId;
        if ($link->query($updateNum) === true) {
            $msg = ['msg' => '添加成功', 'status' => 1];
        } else {
            $msg = ['msg' => '添加失败', 'status' => 0];
        }
        responseEvent($msg);
    } else {
        $insertSql = 'INSERT INTO `shop_cart`(`username`, `pro_id`, `num`, `add_time`) VALUES ("' . $name . '",' . $id . ',' . $num . ',"' . $add_time . '")';
        if ($link->query($insertSql) === true) {
            $msg = ['msg' => '添加成功', 'status' => 1];
        } else {
            $msg = ['msg' => '添加失败', 'status' => 0];
        }
        responseEvent($msg);
    }
}

function chaxunshujuku($selecuSqlStr)
{
    global $link;
    $result = $link->query($selecuSqlStr);

    $arr = [];
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            array_push($arr, $row);
        }
    }
    responseEvent($arr);
}







